package com.library.controllers;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.library.slayer.model.LMSBook;
import com.library.slayer.model.impl.LMSBookImpl;
import com.library.slayer.service.LMSBookLocalServiceUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.util.PortalUtil;

@Controller(value = "AddBookController")  
@RequestMapping("VIEW")  
public class AddBookController {
    @RenderMapping  
    public String handleRenderRequest(RenderRequest request,RenderResponse response,Model model){  
       
     return "view";  
    }
    @RenderMapping(params = "action=details")
    public String details() {
    	return "details";
    }
    @RenderMapping(params = "action=list")
    public String list() {
    	return "list";
    }
    @ActionMapping(params = "action=addBook")
	public void addBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, SystemException,
			PortalException {
		String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
		String author = ParamUtil.getString(actionRequest, "author");

		long bookId = 0L;
		LMSBook book = new LMSBookImpl();

		bookId = CounterLocalServiceUtil.increment(LMSBook.class.getName());
		book.setBookId(bookId);
		book.setBookTitle(bookTitle);
		book.setAuthor(author);
		book.setCreateDate(new Date());

		LMSBookLocalServiceUtil.addLMSBook(book);

		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
		actionResponse.sendRedirect(redirectURL);

	}
	 
	@ActionMapping(params = "action=searchBook")
	public void searchBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		String searchTerm = ParamUtil.getString(actionRequest, "searchTerm");
		try {
			List<LMSBook> books = LMSBookLocalServiceUtil.search(searchTerm);
			actionRequest.setAttribute("SEARCH_RESULT", books);
			actionResponse.setRenderParameter("action","list");
		} catch (SystemException e) {
			e.printStackTrace();
		}
	}
	@ResourceMapping
	public void handleMyResource(ResourceRequest resourceRequest,ResourceResponse resourceResponse){
		try {
			System.out.println("Hello i am inside the resource mapping handleMyResource method");
			HttpServletResponse response = PortalUtil.getHttpServletResponse(resourceResponse);
			response.setContentType("text/plain");
			response.setHeader("Content-Disposition",
			                 "attachment;filename=downloadname.txt");
				 
			int read=0;
			byte[] bytes = "hello my text file".getBytes();
			OutputStream os = response.getOutputStream();
			os.write(bytes, 0, bytes.length);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
		
	}
	 @ActionMapping(params = "action=create")
	 public void create(ActionRequest actionRequest,
				ActionResponse actionResponse) throws IOException, PortletException {
		 QName qName=new QName("http://liferay.com", "userinfo", "x");
		 String bookName = ParamUtil.getString(actionRequest, "bookName");	
			actionResponse.setEvent( qName, bookName);
		}
}